package com.atguigu.flink.chapter05.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author lzc
 * @Date 2023/6/17 15:43
 */
public class DataGenDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        /*DataGeneratorSource<Integer> source = new DataGeneratorSource<>(
            new GeneratorFunction<Long, Integer>() {
                @Override
                public Integer map(Long value) throws Exception {
                    return Math.toIntExact(value);
                }
            },
            100,
            Types.INT
        );
        
        DataStreamSource<Integer> stream = env.fromSource(source,
                                                         WatermarkStrategy.noWatermarks(),
                                                         "data-source"
        );*/
    
        DataGeneratorSource<String> source = new DataGeneratorSource<>(
            new GeneratorFunction<Long, String>() {
                @Override
                public String map(Long value) throws Exception {
                    return "atguigu: " + value;
                }
            },
            Long.MAX_VALUE,
            RateLimiterStrategy.perSecond(1),
            Types.STRING
        );
    
        DataStreamSource<String> stream = env.fromSource(source,
                                                          WatermarkStrategy.noWatermarks(),
                                                          "data-source"
        );
        stream.print();
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
